﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Survivor.src
{
    class StateManager : IManager<State>
    {
        List<State> _states;
        bool _running = true;

        public StateManager()
        {
            _states = new List<State>(10);
        }
        public void AddChild(State s)
        {
            _states.Add(s);
        }
        public bool RemoveChild(State s)
        {
            return _states.Remove(s);
        }
        public void Draw(double dt)
        {
            int size = _states.Count;
            for (int i = size - 1; i >= 0; i--)
            {
                _states[i].Draw(dt);                       
                if (!_states[i].isTransparent) { break; }                                   
            }
        }
        public void Init()
        {
            _states = new List<State>(10);
        }
        public void Load()
        {
            int size = _states.Count;
            for (int i = size - 1; i >= 0; i--)
            {
                if (_states[i].isLoaded == false)
                {
                    _states[i].LoadContent();
                }
                if (!_states[i].isTransparent) { break; }
            }
        }
        public void Update(double dt)
        {
            int size = _states.Count;
            for (int i = size-1; i >= 0; i-- )
            {
                _states[i].Update(dt);                      
                if (!_states[i].isTransparent) { break; }                                 
            }
        }
        public State getState(string name)
        {
            for (int i = 0; i < _states.Count; i++)
            {
                if (_states[i].Name == name)
                {
                    return _states[i];
                }
            }
            return null;
        }

        public void PauseState(string name)
        {
            getState(name).isPaused = true;
        }
        public void UnpauseState(string name)
        {
            getState(name).isPaused = false;
        }
        public void RemoveState(string name)
        {
            RemoveChild(getState(name));
        }
        public void SwitchFocus(State s)
        {
            //??
            //State current = _states[_states.Count - 1];
            //_states = 
        }

        public void Stop()
        {
            _running = false;
        }
        public bool isRunning()
        {
            return _running;
        }
    }
}
